package file

import (
	"errors"
	"github.com/bgmp/config"
	"github.com/bgmp/model"
	"github.com/bgmp/service"
	log "github.com/sirupsen/logrus"
)

func DeleteFile(sid model.Sid) error {
	f := &model.File{}
	f.Sid = sid
	err := f.Get(config.DB)
	if err != nil {
		log.Error(err)
		return err
	}
	err = service.DeleteFile(f.Link)
	if err != nil {
		log.Error(err)
		return errors.New("failed to remove file")
	}
	err = f.Delete(config.DB)
	if err != nil {
		log.Error(err)
		return errors.New("failed to delete file")
	}
	return nil
}
